<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item :label="$t('calling.clinicName')" prop="name">
        <el-input
            v-model="queryParams.name"
            :placeholder="$t('calling.clinicNameInput')"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label-width="60" :label="$t('calling.call_code')" prop="call_code">
        <el-input
            v-model="queryParams.call_code"
            :placeholder="$t('calling.call_codeInput')"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('calling.accessMode')" prop="mode">
        <el-select v-model="queryParams.mode" :placeholder="$t('calling.accessModeSelect')" clearable size="small">
          <el-option key="ads" :label="$t('calling.accessModeSelect1')" value="ads"/>
          <!-- <el-option key="led" :label="$t('calling.accessModeSelect2')" value="led" /> -->
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('calling.loginName')" prop="loginName">
        <el-input
            v-model="queryParams.loginName"
            :placeholder="$t('calling.loginNameInput')"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('program.status')" prop="status">
        <el-select v-model="queryParams.status" :placeholder="$t('equipment.statusSelect')" clearable size="small">
          <el-option
              v-for="dict in statusOptions"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('elButton.establishTime')">
        <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            :start-placeholder="$t('elButton.startTime')"
            :end-placeholder="$t('elButton.endTime')"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{
            $t('elButton.search')
          }}
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('elButton.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            v-hasPerm="['counter_add']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
        >{{ $t('elButton.add') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPerm="['counter_edit']"
            type="success"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleEdit"
        >{{ $t('elButton.change') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPerm="['counter_del']"
            type="danger"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDel"
        >{{ $t('elButton.delete') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPerm="['counter_export']"
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
        >{{ $t('elButton.export') }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            v-hasPerm="['counter_edit']"
            type="success"
            size="mini"
            @click="handleCache"
        >
          <el-tooltip :content="$t('calling.updateCacheContent')" placement="top">
            <i class="el-icon-question"/>
          </el-tooltip>
          {{ $t('calling.updateCache') }}
        </el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" :content="$t('elButton.refresh')" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery"/>
        </el-tooltip>
        <el-tooltip class="item" effect="dark"
                    :content="showSearch ? $t('elButton.hideSearch') : $t('elButton.showSearch')" placement="top"
        >
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch"/>
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="counterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column :label="$t('equipment.number')" width="65" align="center" prop="id"/>
      <el-table-column :label="$t('calling.clinicName')" align="center" prop="name" :show-overflow-tooltip="true"/>
      <el-table-column :label="$t('calling.outpatientSupport')" align="center" prop="bizinfoNames"
                       :show-overflow-tooltip="true"
      />
      <el-table-column :label="$t('calling.accessMode')" align="center" prop="mode">
        <template slot-scope="scope">
          <el-badge :id="scope.id">{{ $t('calling.accessModeSelect1') }}</el-badge>
        </template>
      </el-table-column>
      <el-table-column :label="$t('calling.autoLogin')" align="center" prop="autoLogin" :formatter="autoLoginFormat"/>
      <el-table-column :label="$t('calling.loginName')" align="center" prop="loginName"/>
      <el-table-column :label="$t('calling.lastLogin')" align="center" prop="loginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.loginTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('calling.loginStatus')" align="center" prop="loginStatus" :formatter="loginStatusFormat"/>
      <el-table-column :label="$t('calling.calling')" align="center" prop="calledNum"/>
      <el-table-column :label="$t('program.status')" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column :label="$t('elButton.establishTime')" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('equipment.control')" align="center" width="180"
                       class-name="small-padding fixed-width" fixed="right"
      >
        <template slot-scope="scope">
          <el-button
              v-hasPerm="['counter_rule']"
              size="mini"
              type="text"
              icon="el-icon"
              @click="handleCounterRule(scope.row)"
          >{{ $t('calling.rule') }}
          </el-button>
          <el-button
              v-hasPerm="['counter_edit']"
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
          >{{ $t('elButton.change') }}
          </el-button>
          <el-button
              v-hasPerm="['counter_del']"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDel(scope.row)"
          >{{ $t('elButton.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--诊室规则列表-->
    <el-dialog
        :title="title"
        :visible.sync="openCounterRule"
        :modal-append-to-body="true"
        :append-to-body="true"
        width="60%"
    >
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
              v-hasPerm="['counter_rule_add']"
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleCounterRuleAdd"
          >{{ $t('elButton.add') }}
          </el-button>
        </el-col>
        <div class="top-right-btn">
          <el-tooltip class="item" effect="dark" :content="$t('elButton.refresh')" placement="top">
            <el-button size="mini" circle icon="el-icon-refresh" @click="getCounterRuleList(counterId)"/>
          </el-tooltip>
        </div>
      </el-row>
      <el-table v-loading="loading" :data="counterRuleList" @selection-change="handleSelectionChange">
        <el-table-column :label="$t('equipment.number')" width="55" align="center" prop="id"/>
        <el-table-column :label="$t('calling.style')" align="center" prop="type" :show-overflow-tooltip="true"
                         :formatter="typeFormat"
        />
        <el-table-column :label="$t('program.status')" align="center" prop="status" :formatter="statusRuleFormat"/>
        <el-table-column :label="$t('calling.number')" align="center" prop="num" :show-overflow-tooltip="true"/>
        <!-- <el-table-column align="center" prop="isDefault" :label="$t('calling.isDefault')" width="150">
          <template slot-scope="scope">
            {{ scope.row.isDefault? $t('yes') :$t('no') }}
          </template>
        </el-table-column> -->
        <!-- <el-table-column :label="$t('terminal.startTime')" align="center" prop="startTime" width="150">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.startTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('calling.finishTime')" align="center" prop="finishTime" width="150">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.finishTime) }}</span>
          </template>
        </el-table-column> -->
        <el-table-column :label="$t('equipment.control')" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
                v-hasPerm="['bizinfo_rule_status_edit']"
                size="mini"
                type="text"
                @click="handleBizInfoRuleStatusEdit(scope.row)"
            >
              <span v-if="scope.row.status == 0" style="color:red">{{ $t('elButton.Disable') }}</span>
              <span v-if="scope.row.status == 1">{{ $t('elButton.Enable') }}</span>
            </el-button>
            <el-button
                v-hasPerm="['counter_rule_edit']"
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleCounterRuleEdit(scope.row)"
            >{{ $t('elButton.change') }}
            </el-button>
            <el-button
                v-hasPerm="['counter_rule_del']"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleCounterRuleDel(scope.row)"
            >{{ $t('elButton.delete') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <!--诊室规则新增修改-->
    <el-dialog :title="title" :visible.sync="openCounterRuleAdd" :modal-append-to-body="true" :append-to-body="true"
               :before-close="cancelCounterRule"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <!-- <el-col :span="20">
            <el-form-item :label="$t('calling.effectiveTime')">
              <el-date-picker
                v-model="form.timeInterval"
                style="width: 100%;"
                size="small"
                format="yyyy-MM-dd HH:mm"
                value-format="yyyy-MM-dd HH:mm"
                type="daterange"
                range-separator="-"
                :start-placeholder="$t('elButton.startTime')"
                :end-placeholder="$t('elButton.endTime')"
              />
            </el-form-item>
          </el-col> -->
          <el-col :span="10">
            <el-form-item :label="$t('calling.limitNumber')" prop="num">
              <avue-input-number v-model="form.num" precision="0" controls-position :min-rows="0"/>
              <span class="red">0表示不限制</span>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item :label="$t('calling.eliminateStyle' +
             '')" prop="type">
              <el-select v-model="form.type" :placeholder="$t('calling.styleInput')" style="width: 100%;">
                <el-option
                    v-for="item in [{label:$t('calling.bizinfoAgreement'),value:'1'},
                    {label:$t('calling.eliminateWayDiscarded'),value:'2'},
                    {label:$t('calling.eliminateWayNext'),value:'3'},
                    {label:$t('calling.eliminateWayEnd'),value:'4'},
                    ]"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <!--          <el-col :span="20">-->
          <!--            <el-form-item :label="$t('calling.timeInterval')">-->
          <!--              <el-time-select-->
          <!--                  placeholder="起始时间"-->
          <!--                  v-model="form.startTime"-->
          <!--                  :picker-options="{-->
          <!--      start: '00:00',-->
          <!--      step: '00:30',-->
          <!--      end: '24:00'-->
          <!--    }"-->
          <!--              >-->
          <!--              </el-time-select>-->
          <!--              <el-time-select-->
          <!--                  placeholder="结束时间"-->
          <!--                  v-model="form.finishTime"-->
          <!--                  :picker-options="{-->
          <!--      start: '00:00',-->
          <!--      step: '00:30',-->
          <!--      end: '24:00',-->
          <!--      minTime: form.startTime-->
          <!--    }"-->
          <!--              >-->
          <!--              </el-time-select>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->
          <el-col :span="10">
            <el-form-item :label="$t('program.status')">
              <el-radio-group v-model="form.status">
                <el-radio
                    v-for="dict in [{label:$t('calling.bizinfoRuleStatusEnable'),value:0},{label:$t('calling.bizinfoRuleStatusDisable'),value:1}]"
                    :key="dict.value"
                    :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item :label="$t('calling.isDefault')">
              <el-radio-group v-model="form.isDefault">
                <el-radio
                  v-for="dict in [{label:$t('yes'),value:1},{label:$t('no'),value:0}]"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormCountRule">{{ $t('submitText') }}</el-button>
        <el-button @click="cancelCounterRule">{{ $t('cancelText') }}</el-button>
      </div>
    </el-dialog>
    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.current"
        :limit.sync="queryParams.size"
        @pagination="getList"
    />

    <!-- 添加或修改诊室管理对话框 -->
    <el-dialog
        :title="title"
        :visible.sync="open"
        :modal-append-to-body="true"
        :append-to-body="true"
        :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('calling.clinicName')" prop="name">
              <el-input v-model="form.name" :placeholder="$t('calling.clinicNameInput')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('calling.clinicAlias')" prop="alias">
              <el-input v-model="form.alias" :placeholder="$t('calling.clinicAliasInput')"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('calling.outpatient')" prop="bizinfoIds">

              <el-select v-model="form.bizinfoIds" multiple	 multiple-limit="1" :placeholder="$t('calling.outpatientInput')">
                <el-option
                    v-for="item in bizinfoOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('calling.accessMode')" prop="mode">
              <el-select v-model="form.mode" :placeholder="$t('calling.accessModeSelect')">
                <el-option key="ads" :label="$t('calling.accessModeSelect1')" value="ads"/>
                <!-- <el-option key="led" :label="$t('calling.accessModeSelect2')" value="led" /> -->
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="form.mode == 'ads'" :span="12">
            <el-form-item :label="$t('equipment.identification')" prop="deviceCode">
              <el-input v-model="form.deviceCode" :placeholder="$t('equipment.identificationInput')"/>
            </el-form-item>
          </el-col>
          <el-col v-if="form.mode == 'ads'" :span="12">
            <el-form-item :label="$t('equipment.identification1')" prop="deviceCode">
              <el-input v-model="form.callCode" :placeholder="$t('equipment.identificationInput1')"/>
            </el-form-item>
          </el-col>
          <el-col v-if="form.mode == 'led'" :span="12">
            <el-form-item :label="$t('calling.controlCardType')">
              <el-select v-model="form.cardType" :placeholder="$t('calling.controlCardTypeSelect')" clearable
                         size="small"
              >
                <el-option
                    v-for="dict in cardTpeOptions"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col v-if="form.mode == 'led'" :span="12">
            <el-form-item :label="$t('calling.cardAddress')" prop="cardAddr">
              <el-input v-model="form.cardAddr" :placeholder="$t('calling.cardAddressInput')"/>
            </el-form-item>
          </el-col>
          <el-col v-if="form.mode == 'led'" :span="12">
            <el-form-item :label="$t('calling.cardPort')" prop="cardPort">
              <el-input-number v-model="form.cardPort" controls-position="right" :min="0"/>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item :label="$t('calling.autoLogin')">
              <el-radio-group v-model="form.autoLogin">
                <el-radio
                  v-for="dict in autoLoginOptions"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item :label="$t('program.status')">
              <el-radio-group v-model="form.status">
                <el-radio
                    v-for="dict in statusOptions"
                    :key="dict.value"
                    :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('submitText') }}</el-button>
        <el-button @click="cancel">{{ $t('cancelText') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCounter,
  getCounter,
  delCounter,
  addCounter,
  editCounter,
  updateCounterCache,
  exportCounter,
  listCounterRule,
  getCounterRule,
  addCounterRule,
  editCounterRule,
  delCounterRule,
  editCounterRuleStatus
} from '@/api/qbs/counter'
import { listBizinfo } from '@/api/qbs/bizinfo'

export default {
  name: 'Counter',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      ruleType: [{ label: '和科室一致', value: '1' }, { label: '直接丢弃', value: '2' }, {
        label: '移到下一位',
        value: '3'
      }, { label: '过号废除', value: '4' }],
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 诊室管理表格数据
      counterList: [],
      // 诊室规则数据
      counterRuleList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 诊室规则弹出层
      openCounterRule: false,
      // 日期范围
      dateRange: [], // 接入方式字典
      bizinfoOptions: [], // 自动登录字典
      autoLoginOptions: [], // 登录状态字典
      isDefaultOptions: [], // 是否默认字典
      cardTpeOptions: [],
      loginStatusOptions: [], // 停止状态字典
      pauseStatusOptions: [], // 状态字典
      bizinfoRuleStatusOptions: [], // 查询参数
      statusOptions: [], // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        name: undefined,
        mode: undefined,
        loginName: undefined,
        status: undefined,
        createTime: undefined
      },
      // 显示搜索条件
      showSearch: true,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        num: [
          { required: true, message: this.$t('calling.numtSuccess'), trigger: 'blur' }
        ],
        name: [
          { required: true, message: this.$t('nameEmpty'), trigger: 'blur' }
        ],
        bizinfoIds: [
          {  required: true, message: '所属门诊不能为空', trigger: 'change' }
        ],
        mode: [
          { required: true, message: '接入方式不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getBizinfoList()
    this.getCounterRuleList(this.counterId)
    this.getDicts('yes_no').then(response => {
      this.autoLoginOptions = response.data
    })
    this.getDicts('card_type').then(response => {
      this.cardTpeOptions = response.data
    })
    this.getDicts('status').then(response => {
      this.statusOptions = response.data
    })
    this.getDicts('bizinfo_rule_status').then(response => {
      this.bizinfoRuleStatusOptions = response.data
    })
    this.getDicts('isDefault').then(response => {
      this.isDefaultOptions = response.data
    })
    this.getDicts('login_status').then(response => {
      this.loginStatusOptions = response.data
    })
  },

  methods: {

    /** 查询诊室管理列表 */
    getList() {
      this.loading = true
      listCounter(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.counterList = response.data
        this.total = response.total
        this.loading = false
      })
    },
    // 获取门诊列表
    getBizinfoList() {
      listBizinfo({ current: 1, size: 1000 }).then(response => {
        this.bizinfoOptions = response.data.map(item => {
          item.id = item.id + ''
          return item
        })
      })
    },
    // 自动登录字典翻译
    autoLoginFormat(row, column) {
      return this.selectDictLabel(this.autoLoginOptions, row.autoLogin)
    },
    isDefaultFormat(row, column) {
      return this.selectDictLabel(this.isDefaultOptions, row.isDefault)
    },
    // 登录状态字典翻译
    loginStatusFormat(row, column) {
      return this.selectDictLabel(this.loginStatusOptions, row.loginStatus)
    },
    // 停止状态字典翻译
    pauseStatusFormat(row, column) {
      return this.selectDictLabel(this.pauseStatusOptions, row.pauseStatus)
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    typeFormat(row, column) {
      console.log()
      return this.selectDictLabel(this.ruleType, row.type)
    },
    statusRuleFormat(row, column) {
      return this.selectDictLabel(this.bizinfoRuleStatusOptions, row.status)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 取消按钮
    cancelCounterRule() {
      this.openCounterRuleAdd = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        alias: undefined,
        bizinfoIds: undefined,
        bizinfoNames: undefined,
        mode: 'ads',
        deviceCode: undefined,
        cardType: undefined,
        cardAddr: undefined,
        cardPort: undefined,
        autoLogin: '0',
        loginStatus: '0',
        loginBy: undefined,
        loginName: undefined,
        loginTime: undefined,
        pauseStatus: '0',
        calledNum: undefined,
        deptId: undefined,
        status: '0',
        isDefault: 0
      }
      this.resetForm('form')
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = this.$t('calling.addClinnic')
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      this.reset()
      const id = row.id || this.ids
      getCounter(id).then(response => {
        this.form = response.data
        try {
        this.form.bizinfoIds = this.form.bizinfoIds.split(',')
          
        } catch (error) {
          
        }
        this.open = true
        this.title = this.$t('calling.changeClinnic')
      })
    },
    /** 新增诊室规则按钮操作 */
    handleCounterRuleAdd() {
      this.reset()
      this.openCounterRuleAdd = true
      this.title = this.$t('calling.addClinnicRules')
      // this.form.isDefault = 0
      this.form.status = 1
      this.form.num = 1
    },
    /** 修改按钮操作 */
    handleCounterRuleEdit(row) {
      this.reset()
      const id = row.id || this.ids
      getCounterRule(id).then(response => {
        this.form = response.data
        this.openCounterRuleAdd = true
        this.title = this.$t('calling.changeClinnicRules')
        const dateTemp = []
        dateTemp.push(this.form.startTime)
        dateTemp.push(this.form.finishTime)
        this.$set(this.form, 'timeInterval', dateTemp)
        if (this.form.isDefault === 1) {
          this.form.isDefault = '1'
        } else {
          this.form.isDefault = '0'
        }
      })
    },
    /** 提交按钮 */
    submitFormCountRule: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            // this.form.startTime = this.form.timeInterval[0]
            // this.form.finishTime = this.form.timeInterval[1]

            editCounterRule(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess(this.$t('loginLog.changeSuccess'))
                this.openCounterRuleAdd = false
                this.getCounterRuleList(this.counterId)
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            this.form = {
              counterId: this.counterId,
              type: this.form.type,
              num: this.form.num,
              status: this.form.status,
              // startTime: this.form.timeInterval[0],
              // finishTime: this.form.timeInterval[1],
              isDefault: 0

            }
            addCounterRule(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess(this.$t('addSuccess'))
                this.openCounterRuleAdd = false
                this.getCounterRuleList(this.counterId)
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleCounterRuleDel(row) {
      const ids = row.id || this.ids
      this.$confirm(this.$t('calling.deleteRulesNumber') + ids + this.$t('equipment.deleteEquipment2'), this.$t('warning'), {
        confirmButtonText: this.$t('submitText'),
        cancelButtonText: this.$t('cancelText'),
        type: 'warning'
      }).then(function() {
        return delCounterRule(ids)
      }).then(() => {
        this.getCounterRuleList(this.counterId)
        this.msgSuccess(this.$t('deleteSuccess'))
      }).catch(function() {
      })
    },
    /** 更新规则状态 */
    handleBizInfoRuleStatusEdit(row) {
      // eslint-disable-next-line eqeqeq
      const status = row.status == 0 ? '禁用' : '启用'
      this.$confirm(this.$t('calling.is') + status + this.$t(), this.$t('warning'), {
        confirmButtonText: this.$t('submitText'),
        cancelButtonText: this.$t('cancelText'),
        type: 'warning'
      }).then(function() {
        // eslint-disable-next-line eqeqeq
        return editCounterRuleStatus(row.id, row.counterId, row.status == 0 ? 1 : 0)
      }).then(() => {
        this.getCounterRuleList(this.counterId)
        this.msgSuccess(this.$t('calling.updateSuccess'))
      }).catch(function() {
      })
    },
    /** 规则列表 */
    handleCounterRule(row) {
      this.reset()
      const id = row.id || this.ids
      listCounterRule({ counterId: id }).then(response => {
        this.counterRuleList = response.data
        this.loading = false
        this.openCounterRule = true
        this.title = this.$t('calling.clinicRules')
        this.counterId = id
      })
    },
    getCounterRuleList(id) {
      this.loading = true
      listCounterRule({ counterId: id }).then(response => {
        this.counterRuleList = response.data
        this.loading = false
      })
    },
    /** 更新缓存按钮操作 */
    handleCache(row) {
      updateCounterCache().then(response => {
        this.msgSuccess(this.$t('localUpdateSuccess'))
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.bizinfoNames = this.selectGetName(this.form.bizinfoIds)
          console.log(this.form.bizinfoIds)
          if (Array.isArray(this.form.bizinfoIds)) {
            this.form.bizinfoIds = this.form.bizinfoIds.join()
          }
          if (this.form.id !== undefined) {
            editCounter(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess(this.$t('loginLog.changeSuccess'))
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          } else {
            addCounter(this.form).then(response => {
              if (response.code === 0) {
                this.msgSuccess(this.$t('addSuccess'))
                this.open = false
                this.getList()
              } else {
                this.msgError(response.msg)
              }
            })
          }
        }
      })
    },

    selectGetName(bizinfoIds) {
      var bizinfoArr = []
      for (let i = 0; i < bizinfoIds.length; i++) {
        const obj = this.bizinfoOptions.find((item) => {
          return item.id === bizinfoIds[i]
        })
        this.$set(bizinfoArr, i, obj && obj.name)
      }
      // console.log('选择的门诊', bizinfoArr);
      return bizinfoArr.join()
    },
    /** 删除按钮操作 */
    handleDel(row) {
      const ids = row.id || this.ids
      this.$confirm(this.$t('calling.deleteClinic') + ids + this.$t('equipment.deleteEquipment2'), this.$t('warning'), {
        confirmButtonText: this.$t('submitText'),
        cancelButtonText: this.$t('cancelText'),
        type: 'warning'
      }).then(function() {
        return delCounter(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('deleteSuccess'))
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      if (this.ids.length === 0) {
        this.msgWarning(this.$t('equipment.equipmentInput'))
        return
      }
      const queryParams = this.queryParams
      this.$confirm(this.$t('calling.exportAllclinicData'), this.$t('warning'), {
        confirmButtonText: this.$t('submitText'),
        cancelButtonText: this.$t('cancelText'),
        type: 'warning'
      }).then(() => {
        return exportCounter(queryParams, this.ids)
      }).then(response => {
        this.download(response.data)
      }).catch(function() {
      })
    }
  }
}
</script>
